package vue.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;

import vue.Ui;
import model.ListeTableComboBoxModel;
import controler.Joueur;
import vue.components.*;
/**
 * <p>Title: vue.ToolBar </p>
 *
 * <p>
 * 	Description: Cette classe gere l'affichage de la barre d'outils
 * </p>
 *
 * <p>Copyright: (c) 2008 FSSIM</p>
 *
 * <p>Company: FSSImock</p>
 *
 * @author Antoine Farnault
 * @author Augustin Couette
 * @version 1.0
 * 
 * TODO :: Certaines variables sont a renommer
 */
public class ToolBarPanel extends JToolBar {
	
	private static final long serialVersionUID = 1L;
	// Variables declaration
    private TPButton deconnectButton;
    private TPButton aproposButton;
    public JComboBox tablesSelect;
    private TPLabel jouerLabel;
    // End of variables declaration
    
    public ListeTableComboBoxModel listeTableComboBoxModel = new ListeTableComboBoxModel();
    public Ui ui;
    
    /** Creates new form ToolBarPanel */
    public ToolBarPanel(Ui ui) {
    	this.ui = ui;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {
    	setName("toolBarPanel");
        deconnectButton = new TPButton();
        tablesSelect = new JComboBox();
        aproposButton = new TPButton();
        jouerLabel = new TPLabel();
        
        setBackground(new java.awt.Color(255, 255, 255));
        deconnectButton.setText("Deconnecter");
        deconnectButton.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e){
        		deconnectButtonActionPerformed();
        	}
        });
        
        
        listeTableComboBoxModel.addTableToListe("Voir les tables");
        tablesSelect.setModel(listeTableComboBoxModel);
        tablesSelect.setSelectedIndex(0);
        
        tablesSelect.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e){
        		tablesSelectActionPerformed();
        	}
        });
        
        tablesSelect.setModel(listeTableComboBoxModel);

        aproposButton.setText("A propos");

        jouerLabel.setText("Table : ");

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(deconnectButton)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jouerLabel)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tablesSelect, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 121, Short.MAX_VALUE)
                .addComponent(aproposButton))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(deconnectButton)
                .addComponent(tablesSelect, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(aproposButton)
                .addComponent(jouerLabel))
        );
    }
    /**
     * Action sur le bouton deconnexion
     */
	public void deconnectButtonActionPerformed(){
		if (Joueur.status == Joueur.DISCONNECTED){
				return;
		}
		Joueur.status = Joueur.DISCONNECTED;
		try {
			Joueur.connexion.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		//FIXME vider tout.... detruire les objets
	}
	
	/**
	 * Action sur la combobox de la liste des tables
	 */
	private void tablesSelectActionPerformed() {
		if(tablesSelect.getSelectedItem().equals("Voir les tables")){
			Joueur.ui.cp.layout.show(Joueur.ui.cp, "listTablesEtChatPanel");
			return;
		}
		Joueur.ui.cp.layout.show(Joueur.ui.cp, "tablePanel_"+tablesSelect.getSelectedItem());
	}
}


